package com.dwb.service;

import com.dwb.entity.SOrder;
import com.dwb.entity.SOrderAddress;
import com.dwb.entity.SOrderDetail;
import com.dwb.mappers.SOrderAddressMapper;
import com.dwb.mappers.SOrderDetailMapper;
import com.dwb.mappers.SOrderMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author manke
 * @Date 2020/6/27 14:57
 * @Version 0.0.1
 */
@Service
public class SOrderService {
    @Autowired
    private SOrderMapper sOrderMapper;
    @Autowired
    private SOrderAddressMapper sOrderAddressMapper;
    @Autowired
    private SOrderDetailMapper sOrderDetailMapper;

    public PageInfo<SOrder> getList(Map<String, String> parameters) {
        PageHelper.startPage(Integer.parseInt(parameters.get("pageNum") == null ? "1" : parameters.get("pageNum")),
                Integer.parseInt(parameters.get("pageSize")==null ? "10" : parameters.get("pageSize")));
        Example example = new Example(SOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("orderNo", "%" + (parameters.get("orderNo") == null ? "" : parameters.get("orderNo")) + "%")
                .andLike("userId", "%" + (parameters.get("userId") == null ? "" : parameters.get("userId")) + "%");
        List<SOrder> sOrders = sOrderMapper.selectByExample(example);
        return new PageInfo<>(sOrders);

    }


    public Map<String, Object> getInfo(Map<String, Object> parameters) {

        // 订单基本信息表
        SOrder sOrder = sOrderMapper.selectByPrimaryKey(parameters.get("id"));
        //订单地址
        Example example = new Example(SOrderAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId",sOrder.getId());
        List<SOrderAddress> sOrderAddress = sOrderAddressMapper.selectByExample(example);
        // 订单详情表
        Example exampleV = new Example(SOrderDetail.class);
        Example.Criteria criteria1 = exampleV.createCriteria();
        criteria1.andEqualTo("orderId",sOrder.getId());
        List<SOrderDetail> sOrderDetails = sOrderDetailMapper.selectByExample(exampleV);

        HashMap<String, Object> map = new HashMap<>();
        map.put("sOrder",sOrder);
        map.put("sOrderAddress",sOrderAddress);
        map.put("sOrderDetails",sOrderDetails);
        return map;

    }
    public void update(Map<String, Object> parameters) {
        SOrder sOrder = new SOrder();
        sOrder.setId(Integer.parseInt(parameters.get("id").toString()));
        sOrder.setStatus(new Byte(parameters.get("state").toString()));
        sOrderMapper.updateByPrimaryKeySelective(sOrder);
    }
}
